package com.ningxun.rms.web.controller.signup;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ningxun.rms.domain.basewechatuser.BaseWechatUser;
import com.ningxun.rms.domain.busrequire.BusRequire;
import com.ningxun.rms.service.baserequire.IBusRequireService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ningxun.rms.common.annotation.Log;
import com.ningxun.rms.common.core.controller.BaseController;
import com.ningxun.rms.common.core.domain.AjaxResult;
import com.ningxun.rms.common.enums.BusinessType;
import com.ningxun.rms.signup.domain.BusSignup;
import com.ningxun.rms.signup.service.IBusSignupService;
import com.ningxun.rms.common.utils.poi.ExcelUtil;
import com.ningxun.rms.common.core.page.TableDataInfo;

/**
 * 报名信息Controller
 *
 * @author rms
 * @date 2025-07-15
 */
@RestController
@RequestMapping("/signup/signup")
public class BusSignupController extends BaseController
{
    @Autowired
    private IBusSignupService busSignupService;
    @Autowired
    private IBusRequireService busRequireService;
    /**
     * 查询报名信息列表
     */
    @PreAuthorize("@ss.hasPermi('signup:signup:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusSignup busSignup)
    {
        startPage();
        List<BusSignup> list = busSignupService.selectBusSignupList(busSignup);
        return getDataTable(list);
    }

    /**
     * 导出报名信息列表
     */
    @PreAuthorize("@ss.hasPermi('signup:signup:export')")
    @Log(title = "报名信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusSignup busSignup)
    {
        List<BusSignup> list = busSignupService.selectBusSignupList(busSignup);
        ExcelUtil<BusSignup> util = new ExcelUtil<BusSignup>(BusSignup.class);
        util.exportExcel(response, list, "报名信息数据");
    }

    /**
     * 获取报名信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('signup:signup:query')")
    @GetMapping(value = "/{signupId}")
    public AjaxResult getInfo(@PathVariable("signupId") Long signupId)
    {
        return success(busSignupService.selectBusSignupBySignupId(signupId));
    }

    /**
     * 新增报名信息
     */
    @PreAuthorize("@ss.hasPermi('signup:signup:add')")
    @Log(title = "报名信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusSignup busSignup)
    {
        BusRequire busRequire = busRequireService.selectBusRequireByRequireId(busSignup.getRequireId());
        if(busRequire.getQuantity()<=0){
            return error("该岗位已满员");
        }
        return toAjax(busSignupService.insertBusSignup(busSignup));
    }

    /**
     * 修改报名信息
     */
    @PreAuthorize("@ss.hasPermi('signup:signup:edit')")
    @Log(title = "报名信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusSignup busSignup)
    {

        return toAjax(busSignupService.updateBusSignup(busSignup));
    }

    /**
     * 删除报名信息
     */
    @PreAuthorize("@ss.hasPermi('signup:signup:remove')")
    @Log(title = "报名信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{signupIds}")
    public AjaxResult remove(@PathVariable Long[] signupIds)
    {
        return toAjax(busSignupService.deleteBusSignupBySignupIds(signupIds));
    }

    /**
     * 查询报名是否成功
     * @param openId
     * @return
     */
    @GetMapping("/success")
    @Scheduled(cron = "*/5 * * * * ?")
    public AjaxResult success(@PathVariable String openId)
    {
        return new AjaxResult(200,String.valueOf(busSignupService.selectIsSuccess(openId)));
    }
}
